/**
 * 2009-10-27 下午04:37:23 
 * esignature
 * com.smc.dao.impl
 * HospitalDaoImpl
 */
package com.smc.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.smc.dao.HospitalDao;
import com.smc.domain.model.Hospital;

/**
 * @author chenheng
 * @version 0.0.1
 * 持久层医院接口实现
 */
public class HospitalDaoImpl extends HibernateDaoSupport implements HospitalDao {

	public void saveHospital(Hospital hospital) {
		getHibernateTemplate().saveOrUpdate(hospital);
	}

	@SuppressWarnings("unchecked")
	public List<Hospital> fetch_OP_HO_FE_hospital() {
		String hql = "SELECT new Hospital(uuid,name) FROM Hospital";
		List<Hospital> hl = getHibernateTemplate().find(hql);
		return hl ;
	}

	@SuppressWarnings("unchecked")
	public List<Hospital> fetchAllHospital() {
		String hql = "SELECT new Hospital(uuid,name) FROM Hospital";
		List<Hospital> hl = getHibernateTemplate().find(hql);
		return hl ;
	}

	public int countHospital() {
		String hql = "SELECT COUNT(a) FROM Hospital a" ;
		int count = 0 ;
		List l = getHibernateTemplate().find(hql);
		if ( l.size() > 0 ) {
			count = ((Long) l.iterator().next()).intValue();
		}
		return count;
	}

	@SuppressWarnings("unchecked")
	public List<Hospital> fetchAllHospital(int pageIndex, int pageSize) {
		Session session = getSession();
		String hql = " FROM Hospital " ;
		Query query = session.createQuery(hql);
		query.setFirstResult((pageIndex-1)*pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}
	
}
